home *** CD-ROM | disk | FTP | other *** search
- From: "Oliver Laumann" <talcott!seismo!unido!tub!net>
- Subject: tm_to_time(3) -- convert broken-down time into time_t.
- Newsgroups: mod.sources
- Approved: jpn@panda.UUCP
-
- Mod.sources: Volume 4, Issue 38
- Submitted by: "Oliver Laumann" <seismo!unido!tub!net>
-
-
- A number of requests have been posted to the net in the past asking
- for a function that can be used to convert a broken-down time (such
- as returned by localtime()) into a time_t (such as returned by
- time()).
- The following shell archive contains a function (plus manual page)
- that performs this conversion.
-
- Regards,
- Oliver Laumann, Technical University of Berlin, Germany.
- ...ihnp4!seismo!unido!tub!net or net@DB0TUI6.BITNET
- ...!mcvax!unido!tub!net
-
- ---------------cut here---------------cut here---------------
- #! /bin/sh
- # This is a shell archive, meaning:
- # 1. Remove everything above the #! /bin/sh line.
- # 2. Save the resulting text in a file.
- # 3. Execute the file with /bin/sh (not csh) to create the files:
- # tm_to_time.3
- # tm_to_time.c
- # This archive created: Wed Mar 19 13:04:07 1986
- export PATH; PATH=/bin:$PATH
- echo shar: extracting "'tm_to_time.3'" '(683 characters)'
- if test -f 'tm_to_time.3'
- then
- echo shar: will not over-write existing file "'tm_to_time.3'"
- else
- cat << \SHAR_EOF > 'tm_to_time.3'
- .TH MKTIME 3 "19 March 1986"
- .SH NAME
- tm_to_time \- convert broken-down time into time_t
- .SH SYNOPSIS
- .nf
- .B #include <sys/types.h>
- .B #include <sys/time.h>
- .PP
- .B time_t tm_to_time (tp);
- .B struct tm *tp;
- .fi
- .SH DESCRIPTION
- .I Tm_to_time
- converts a broken-down time pointed to by
- .I tp
- (such as returned by
- .IR localtime )
- into a
- .I time_t
- (that is, a number of seconds since 00:00:00 GMT, Jan 1, 1970).
- Thus, if
- .I tm_to_time
- is called with a broken-down time
- returned by a call to
- .I localtime
- with some
- .I time_t
- argument,
- the value obtained by
- .I tm_to_time
- is identical to the original
- .IR time_t .
- .SH AUTHOR
- Oliver Laumann
- .SH SEE ALSO
- gettimeofday(2) or time(2), ctime(3).
- SHAR_EOF
- if test 683 -ne "`wc -c < 'tm_to_time.3'`"
- then
- echo shar: error transmitting "'tm_to_time.3'" '(should have been 683 characters)'
- fi
- fi # end of overwriting check
- echo shar: extracting "'tm_to_time.c'" '(1192 characters)'
- if test -f 'tm_to_time.c'
- then
- echo shar: will not over-write existing file "'tm_to_time.c'"
- else
- cat << \SHAR_EOF > 'tm_to_time.c'
- #include <sys/types.h>
- #include <sys/time.h>
-
- /* Return 1 if `y' is a leap year, 0 otherwise.
- */
-
- static int leap (y) int y; {
- y += 1900;
- if (y % 400 == 0)
- return (1);
- if (y % 100 == 0)
- return (0);
- return (y % 4 == 0);
- }
-
- /* Return the number of days between Jan 1, 1970 and the given
- * broken-down time.
- */
-
- static int ndays (p) struct tm *p; {
- register n = p->tm_mday;
- register m, y;
- register char *md = "\37\34\37\36\37\36\37\37\36\37\36\37";
-
- for (y = 70; y < p->tm_year; ++y) {
- n += 365;
- if (leap (y)) ++n;
- }
- for (m = 0; m < p->tm_mon; ++m)
- n += md[m] + (m == 1 && leap (y));
- return (n);
- }
-
- /* Convert a broken-down time (such as returned by localtime())
- * back into a `time_t'.
- */
-
- time_t tm_to_time (tp) struct tm *tp; {
- register int m1, m2;
- time_t t;
- struct tm otm;
-
- t = (ndays (tp) - 1) * 86400L + tp->tm_hour * 3600L
- + tp->tm_min * 60 + tp->tm_sec;
- /*
- * Now the hard part -- correct for the time zone:
- */
- otm = *tp;
- tp = localtime (&t);
- m1 = tp->tm_hour * 60 + tp->tm_min;
- m2 = otm.tm_hour * 60 + otm.tm_min;
- t -= ((m1 - m2 + 720 + 1440) % 1440 - 720) * 60L;
- return (t);
- }
- SHAR_EOF
- if test 1192 -ne "`wc -c < 'tm_to_time.c'`"
- then
- echo shar: error transmitting "'tm_to_time.c'" '(should have been 1192 characters)'
- fi
- fi # end of overwriting check
- # End of shell archive
- exit 0
-
-